package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.myapplication.bean.Weather;
import com.example.myapplication.bean.WeatherData;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import static android.R.attr.name;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tvName = (TextView) findViewById(R.id.tv_name);
        //HashMap<String,String>map= (HashMap<String, String>) getDataFromJason();
        //tvName.setText(map.get("name"));
        String wd=getDataFromJason();
        tvName.setText(wd);
    }

    private String getDataFromJason() {
        //
        InputStream in=getResources().openRawResource(R.raw.weather);
        byte[]buf= new byte[0];
        try {
            buf = new byte[in.available()];
        } catch (IOException e) {
            e.printStackTrace();
        }
        String data="";
        String age="";
        String high="";
        String ymd="";
        try {
            int len=in.read(buf);
            data=new String (buf,0,len);
            Gson gson=new Gson( );
            Weather w=(Weather)gson.fromJson(data, Weather.class);
            ymd=w.getData().getYesterday().getYmd();
//            ymd=w.getData().getForecast().get(0).getYmd();
            JSONObject jsonObject=new JSONObject(data).getJSONObject("data");
            data=jsonObject.getString("wendu");
            JSONArray jsonArray= jsonObject.getJSONArray("forecast");
            JSONObject fore1= (JSONObject) jsonArray.get(0);

            high=fore1.getString("high");

        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
//        HashMap<String,String> map=new HashMap<>();
//        map.put("name",name);
//        map.put("age",age);

        return ymd;
    }
}
